﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

namespace Ebiz
{
    public partial class DownloadClaims : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["data"] != null)
            {
                if (Request.Cookies["iddata"] != null)
                {
                    string path = Server.MapPath("~/images/upload/");
                    string filename = "claims-" + Request.Cookies["iddata"].Value + ".xml";
                    Request.Cookies["iddata"].Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(Request.Cookies["iddata"]);
                    if (File.Exists(path + filename))
                    {
                        DataTable dt = new DataTable();
                        dt.ReadXml(path + filename);
                        dt.Columns.Remove("IDData");
                        GridView gridviewResult = new GridView();
                        gridviewResult.DataSource = dt;
                        gridviewResult.DataBind();
                        gridviewResult.PageIndex = 0;
                        StringWriter sw = new StringWriter();
                        HtmlTextWriter hw = new HtmlTextWriter(sw);
                        gridviewResult.RenderControl(hw);


                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=ClaimsCheckingResult.xlsx");
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.ms-excel";
                        Response.Output.Write(sw.ToString());
                        Response.Flush();
                        Response.End();
                    }
                }
            }
        }
    }
}
